"""
余额分账相关服务
"""

from jlpay.services.base_service import BaseService
from jlpay.model.ledger.balancesplit import (
    BalanceQueryRequest, BalanceQueryResponse,
    BalanceSplitRequest, BalanceSplitResponse,
    BalanceSplitQueryRequest, BalanceSplitQueryResponse,
    BalanceRefundRequest, BalanceRefundResponse,
    BalanceRefundQueryRequest, BalanceRefundQueryResponse
)


class BalanceSplitService(BaseService):
    """余额分账服务"""
    
    def __init__(self, config):
        """
        初始化余额分账服务
        
        Args:
            config: OrgConfig实例
        """
        super().__init__(config)
    
    def get_api_url(self, request_class, api_path=None):
        """
        获取API URL
        
        Args:
            request_class: 请求类
            api_path: 接口路径
            
        Returns:
            完整的API URL
        """
        base_url = self.config.url
        
        if api_path:
            return f"{base_url}{api_path}"
        
        # 直接使用Request类中定义的完整API路径
        api_url = getattr(request_class, 'API_URL', '')
        
        if not api_url:
            # 如果没有定义API_URL，使用默认路径
            return f"{base_url}/open/ledger/balancesplit"
        
        # 直接返回完整路径
        return f"{base_url}{api_url}"
    
    def query_balance(self, request):
        """
        余额查询
        
        Args:
            request: BalanceQueryRequest实例
            
        Returns:
            BalanceQueryResponse实例
        """
        return self.post(request, BalanceQueryResponse)
    
    def split_balance(self, request):
        """
        余额分账
        
        Args:
            request: BalanceSplitRequest实例
            
        Returns:
            BalanceSplitResponse实例
        """
        return self.post(request, BalanceSplitResponse)
    
    def query_balance_split(self, request):
        """
        余额分账查询
        
        Args:
            request: BalanceSplitQueryRequest实例
            
        Returns:
            BalanceSplitQueryResponse实例
        """
        return self.post(request, BalanceSplitQueryResponse)
    
    def refund_balance(self, request):
        """
        余额退款
        
        Args:
            request: BalanceRefundRequest实例
            
        Returns:
            BalanceRefundResponse实例
        """
        return self.post(request, BalanceRefundResponse)
    
    def query_balance_refund(self, request):
        """
        余额退款查询
        
        Args:
            request: BalanceRefundQueryRequest实例
            
        Returns:
            BalanceRefundQueryResponse实例
        """
        return self.post(request, BalanceRefundQueryResponse) 